package com.maciek.attention;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;

public class GroupView extends Activity {
	
	private ArrayList<String> groups;
	private ArrayAdapter<String> adapter;
	private Intent explicitIntent;
	private DatabaseHelper dbHelper;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.groupview);
        
        this.dbHelper = new DatabaseHelper(this);
        this.dbHelper.insert("Groep1a");
        this.dbHelper.insert("Groep1b");
        this.dbHelper.insert("Groep2a");
        
        //ListView to display the groups
        ListView groupList = (ListView)findViewById(R.id.groupListView);
        groupList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				explicitIntent = new Intent(GroupView.this, MemberView.class);
				startActivity(explicitIntent);
			}
		});

        
        //ArrayList containing the groups
        groups = dbHelper.selectAll();
        
        adapter = new ArrayAdapter<String>(	this, 
        									android.R.layout.simple_list_item_1, 
        									groups);
         
        //Set the adapter for our groupList
        groupList.setAdapter(adapter);
         
        //groups.add("Groep 1a");
    	//groups.add("Groep 1b");
    	//groups.add("Groep 2a");
    	adapter.notifyDataSetChanged();
	    
    	//Register our listView for the contextMenu.
    	//Otherwise, the contextMenu wont show.
    	registerForContextMenu(groupList);
        
    }
    
    /**
     * Method to create the options menu which opens
     * when the menu button is pressed.
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
    	super.onCreateOptionsMenu(menu);
    	
    	menu.add(0, 0, 0, "New group");
    	return true;
    }
    
    /**
     * Method that handles the selection
     * of items that are listen in the 
     * options menu.
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
    	switch (item.getItemId()){
    		case 0: addGroup();
    	}
    	return true;
    }
    
    /**
     * Method to create a contextMenu which
     * is displayed when the user presses
     * an item from the list for a short time.
     */
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    	menu.setHeaderTitle("Group options");
    	menu.add(0, 0, 0, "Edit name");
    	menu.add(0, 1, 0, "Delete group");
    }
    
    /**
     * Method to handle the selection
     * of contextMenu items.
     */
    @Override
    public boolean onContextItemSelected(MenuItem item){
    	AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
    	switch (item.getItemId()){
    	case 0: explicitIntent = new Intent(GroupView.this, MemberView.class);
				startActivity(explicitIntent);
				return true;
    	case 1: explicitIntent = new Intent(GroupView.this, MemberView.class);
				startActivity(explicitIntent);
				return true;
    	default:
    	    return super.onContextItemSelected(item);
    	  }
    }
    
    /**
     * Method to add a new group to the
     * existing ones. It will open a notification
     * window and ask the user to fill in a group name.
     */
    public void addGroup(){
    	//create the notification window
    	Builder popup = new AlertDialog.Builder(this);
    	
    	//give the popup a title
    	popup.setTitle("New group");
    	
    	//set a message to display in the window
		popup.setMessage("Enter a group name");
    	
		//create a exitText view to receive input
		//this EditText must be final, otherwise
		//eclipse starts muttering when we extract values
		final EditText textField = new EditText(this);
		
		//add the input field to the notification window
		popup.setView(textField);
		
		//add a confirmation button to the notification window
		popup.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				String input = textField.getText().toString();
				groups.add(input);
				adapter.notifyDataSetChanged();
			}
		});
		
		popup.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				//leave this listener empty
			}
		});
		popup.show();
    }

}